<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */

    /**
     * Defines the Media model of the application.
     *
     * @package     application
     * @subpackage  models
     *
     * @author NairuS
     * @version 1.2 $Rev: 90 $ $Date: 2011-01-16 09:23:26 +0000 (Sun, 16 Jan 2011) $ $Author: nicolas.surian $
     */
    class UserModel extends Model
    {
        /**
         * Defines the name of block property.
         *
         * @var string
         */
        const BLOCK_FIELD = 'block';

        /**
         * Defines the name of email property.
         *
         * @var string|array
         */
        const EMAIL_FIELD  = 'email';

        /**
         * Defines the name of firstName property.
         *
         * @var string|array
         */
        const FIRST_NAME_FIELD  = 'firstName';

        /**
         * Defines the name of lastName property.
         *
         * @var string
         */
        const LAST_NAME_FIELD = 'lastName';

        /**
         * Defines the name of password property.
         *
         * @var string
         */
        const PASSWORD_FIELD = 'password';

        /**
         * Defines the name of roleID property.
         *
         * @var string
         */
        const ROLE_ID_FIELD = 'roleID';

        /**
         * Defines the name of userName property.
         *
         * @var string
         */
        const USER_NAME_FIELD = 'userName';

        /**
         * Invoked before the validate method.
         *
         * @return void
         */
        public function beforeValidate()
        {
            if( $this->isNew() )
            {
                $this->setExcludeFields( array( self::ID_FIELD ) );
            }
            else
            {
                // if the password is empty to not be changed, we exclude it from the validation process.
                if( $this->getVO()->password == '' )
                {
                    $this->setExcludeFields( array( self::PASSWORD_FIELD ) );
                }

                // add except option if it's not a new object.
                $userNameValidator = $this->getPostValidator( self::USER_NAME_FIELD );
                $this->setOption( UniqueValidator::$exceptKey, array( self::ID_FIELD => $this->vo->id ), $userNameValidator );
            }
        }

        /**
         * Configure the validation of this model.
         *
         * @param array $exludeFields
         * @return void
         */
        public function configure( $exludeFields = array() )
        {
            // sets the validators.
            $stringOptions = array( StringValidator::$maxLengthKey => 50 ) ;
            $this->setValidators(
                array(
                      self::BLOCK_FIELD      => new BooleanValidator()
                    , self::EMAIL_FIELD      => new EmailValidator( $stringOptions )
                    , self::FIRST_NAME_FIELD => new StringValidator( $stringOptions )
                    , self::LAST_NAME_FIELD  => new StringValidator( $stringOptions )
                    , self::PASSWORD_FIELD   => new StringValidator( array_merge( $stringOptions, array( StringValidator::$minLengthKey => 8) ) )
                    , self::ROLE_ID_FIELD    => new ChoiceDatabaseValidator( array( ChoiceDatabaseValidator::$fieldKey => self::ROLE_ID_FIELD, ChoiceDatabaseValidator::$foreignKey => RoleModel::ID_FIELD, ChoiceDatabaseValidator::$tableKey => "kp_roles"))
                    , self::USER_NAME_FIELD  => new RegexValidator( array( RegexValidator::$patternKey => '/^[a-z]+[a-z0-9_-]{0,}$/i') )
                )
            );

            $this->setPostValidator( self::USER_NAME_FIELD, new UniqueValidator( array( UniqueValidator::$fieldKey => self::USER_NAME_FIELD, UniqueValidator::$tableKey => 'kp_users' ) ) );

            // defines the extra fields to not validate.
            parent::configure( $exludeFields ) ;

        }

        /**
         * Returns the UserVO of the model.
         *
         * @return UserVO
         */
        public function getVO()
        {
            return $this->vo;
        }
    }
?>